<?php
	class Caller {
		var $controller;
		var $method;
		var $params;
		var $code;
		function Caller($controller, $function, $params) {
			$this->controller = $controller;
			$this->method = $function;
			$this->params = $params;
			
			if(!$this->controllerExists()) {
				return false;
			}
			if(!$this->methodExists()) {
				return false;
			}
			$this->call();
			return true;
		}
		function call() {
			$controller = $this->controller;
			$params = $this->params;
			$method = $this->method;
			
			include CONTROLLER_PATH."$controller.php";
			$p = arrayToNumericIndex($params);
			
			/* 
			 * Let's instantiate the requested controller
			 */
			$control = @eval("return new $controller();");
			
			if(!$control) {
				echo "<br><em>$controller</em>does not exist ";
			}
				
			if(!method_exists($control, $method)) {
				echo "<br>Method <em>$method</em> does not exist in <em>$controller</em>";
				return 0;
			}
			/*
			 * Now transfer the control
			 */
			 
			call_user_func_array( array($control, $method), $this->params);
		}
		function controllerExists() {
			if(!file_exists(CONTROLLER_PATH . "{$this->controller}.php")) {
				echo "<br>ERROR: The controller [{$this->controller}] does not exist";			
				return false;
			}
			else
				return true;
		}
		function methodExists() {
			/*
			 * Find out if this method exists
			 * Note: haven't implemented this yet
			 */
			return true;
		}
		
	}
?>
